/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.scenic.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.scenic.entity.ScenicSpot;
import com.rzico.scenic.service.ScenicSpotService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 旅游景区控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "旅游景区接口")
@RestController
@RequestMapping("/admin/scenicSpot")
public class ScenicSpotAdminController extends BaseController {

    @Autowired
    private ScenicSpotService scenicSpotService;

    /**
     * 分页查询旅游景区
     *
     * @return
    */
    @ApiOperation("分页查询旅游景区")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "搜索内容", dataType = "String", paramType = "query")
    })
    public CommResult<ScenicSpot> list(Date startDate, Date endDate, Pageable pageable, String name) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ScenicSpot> list = scenicSpotService.selectList(params);
        PageResult<ScenicSpot> pageResult = new PageResult<ScenicSpot>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条旅游景区
     *
     * @return
    */
    @ApiOperation("查询单条旅游景区")
    @GetMapping("/find/{id}")
    public CommResult<ScenicSpot> find(@PathVariable String id) {
        ScenicSpot result = scenicSpotService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("scenicSpot",result);
        return CommResult.success(data);

    }

    /**
     * 保存旅游景区
     *
     * @param scenicSpot
     * @return
     */
    @ApiOperation("保存旅游景区")
    @PostMapping("/save")
    public CommResult<ScenicSpot> save(@RequestBody ScenicSpot scenicSpot) {
        int affectCount = scenicSpotService.insertUseGeneratedKeys(scenicSpot);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        ScenicSpot result = scenicSpotService.findById(scenicSpot.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除旅游景区
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除旅游景区", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除旅游景区,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<ScenicSpot> del(@PathVariable String ids) {

        scenicSpotService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新旅游景区
     *
     * @param scenicSpot
     * @return
     */
    @Log(desc = "更新旅游景区", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新旅游景区")
    @PostMapping("/update")
    public CommResult<ScenicSpot> update(@RequestBody ScenicSpot scenicSpot) {
        int affectCount = scenicSpotService.updateByPrimaryKeySelective(scenicSpot);
        if (affectCount <= 0){
            return CommResult.error();
        }
        ScenicSpot result = scenicSpotService.findById(scenicSpot.getId());
        return CommResult.success(result);
    }

    @Log(desc = "更新景区排序", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新景区排序")
    @PostMapping("/updateOrders/{id}/{orders}")
    public CommResult<ScenicSpot> updateOrders(@PathVariable String id,@PathVariable Integer orders) {
        ScenicSpot scenicSpot = scenicSpotService.findById(id);
        if (scenicSpot != null) {
            scenicSpot.setOrders(orders);
            int affectCount = scenicSpotService.updateByPrimaryKeySelective(scenicSpot);
            if (affectCount <= 0){
                return CommResult.error();
            }
            return CommResult.success(scenicSpot);
        } else {
            return CommResult.error();
        }
    }

}
